package com.foo.common.base.config;

import java.util.Map;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import com.google.common.collect.Maps;

/**
 * In Servlets 3.0 you don't need a web.xml for many cases, however, sometimes it's required or just useful.
 *
 */
public class WebAppInitializer implements WebApplicationInitializer {

	Logger logger = LoggerFactory.getLogger(WebAppInitializer.class);

	public void onStartup(ServletContext container) throws ServletException {

		// Create the 'root' Spring application context
		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
		// rootContext.register(ResourcesConfig.class);
		rootContext.register(CoreConfig.class);

		// Add filter
		FilterRegistration struts2Filter = container.addFilter("struts2",
				org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter.class);

		Map<String, String> map = Maps.newHashMap();
		map.put("actionPackages", "com.base.common.base.action");
		map.put("loggerFactory",
				"com.opensymphony.xwork2.util.logging.slf4j.Slf4jLoggerFactory");

		struts2Filter.setInitParameters(map);
		struts2Filter.addMappingForUrlPatterns(null, false, "/*");

		// add Listener
		container.addListener(new ContextLoaderListener(rootContext));

	}

}